package com.example.webdome.controller;

import com.example.webdome.pojo.Result;
import com.example.webdome.service.SetDataService;
import com.example.webdome.util.getCookie;
import com.example.webdome.util.urlCount;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/SetData")
public class SetDataController {
    @Resource
    private SetDataService setDataService;
    @Resource
    private getCookie getCookie;
    @Resource
    private urlCount Count;
    //保存设置信息
    @PostMapping("/saveSetData")
    Result saveSetData(@RequestParam String jsonData, HttpServletRequest request) {
        String uri = request.getRequestURI();
        Count.recordUrlCount(uri);
        String cookieValue = request.getHeader("Cookie");
        String token = getCookie.getCookieValue(cookieValue);
            try {
                if (token != null) {
                    return setDataService.saveSetData(jsonData, token);
                }
            } catch (Exception e) {
                return Result.failed("保存失败：" + e.getMessage());
            }
        return Result.failed("请先登录！");
    }
    //获取用户当前修改设置
    @GetMapping("/getSetData")
    public Result getUserSetData( HttpServletRequest request){
        String cookieValue = request.getHeader("Cookie");
        String token = getCookie.getCookieValue(cookieValue);
        try {
            if (token != null) {
                return setDataService.getUserSetData(token);
            }
        } catch (Exception e) {
            return Result.failed("加载失败：" + e.getMessage());
        }
        return Result.failed("请先登录！");
    }
}
